<?php
/**
 * Class for work with database table "member_accounts"
 * 
 * @version 2012-10-10
 * @package model
 * @subpackage accounts
 * @category Db model
 */

class MemberAccountsModel extends Db_Table
{
	/**
     * __construct
     * This construct function connects to the table "Member_Accounts".
     *
     * @version 2012-10-10
     * @uses parent::__construct('member_accounts')
     * @return object
     */
	function __construct()
	{
		parent::__construct("member_accounts");
	}
	
	 /**    
     * getMemberList
     * This function returns an array of records with member.
     *
     * @version 2013-01-18 
     * @param array $where filter conditions
     * @param array $sort sorting object     
     * @param int $pager | pager object
     * @return array rows filtered records
     * @access public
     */
	public function getMemberList($where=array(), $pager=10000, $sort=array(), $isNew = true) 
	{
		$map1 = $this->fields;
        foreach ($map1 as $key=>$val)
			$map[$key] = 'm.'.$val;
		$map['ip'] = 'INET_NTOA(m.last_ip)';
		$sql = 'SELECT COUNT(*) FROM '.$this->table.' AS m ' 
			.'WHERE '.$this->_parseWhere($where, $map);
        $cnt = $this->db->getField($sql);
		if (!$cnt)
            return array();
		if ($pager){
            if (is_object($pager))
                $limit = $pager->init($cnt);
            else 
                $limit = intVal($pager);
        }

		$sql = "SELECT "
            .$this->_joinFields($map)
            ." FROM ".$this->table." as m "
			."WHERE ".$this->_parseWhere($where, $map)." "
            .$this->_parseOrder($sort)
            ." LIMIT ".$limit."";
		return $this->db->getRows($sql);
	}
	
	 /**
     * addMember
     * This function gets the data about a member and inserts the data into the table "member_accounts".
     *
     * @version 2013-01-22
     * @param array $data     
     * @return array
     * @access public
     */
    public function addMember($login,$pass,$email)
    {
		$dbKeys = Db::getModel('keys');
		$sql = 'CALL sp_create_new_user("'.$login.'","'._pass($pass).'","member","'.$email.'")';
		$result = $this->db->getRow($sql);
			$dbKeys->AddKey($result['uuid']);
		return $result;
    }

	/**
     * addAdmin
     * This function gets the data about a member and inserts the data into the table "member_accounts".
     *
     * @version 2013-01-25
     * @param array $data     
     * @return array
     * @access public
     */
    public function addAdmin($login,$pass,$email)
    {
		$dbKeys = Db::getModel('keys');
		$sql = 'CALL sp_create_new_user("'.$login.'","'._pass($pass).'","admin","'.$email.'")';
		return $this->db->getRow($sql);
    }
	
	/**
	* blocked
    * This fucnction changes the status of an account to 'blocked'.
	*
	* @version 2013-01-18
	* @param integer $id
	* @param integer $blocked_id
	* @return boolean
	* @access public
	*/
	public function blocked($uuid)
	{
		$cfg = array(
			'blocked'=>0
		);
		return $this->update($cfg,$uuid);
	}	
	
	/**
	* unblocked
    * This fucnction changes the status of an account to 'active'.
	*
	* @version 2013-01-18
	* @param integer $id
	* @return boolean
	* @access public
	*/
	public function unblocked($uuid)
	{
		$cfg = array(
			'blocked'=>3
		);
		return $this->update($cfg,$uuid);
	}

	/**
	* delete
    * This fucnction remove account.
	*
	* @version 2013-01-21
	* @param integer $id
	* @return boolean
	* @access public
	*/
	public function delete($uuid)
	{
		$dbKeys = Db::getModel('keys');
		$where = array(
			'uuid'=>"='".$uuid."'"
		);
		$dbKeys->delKey($uuid);
		return $this->deleteBy($where);
	}
}